﻿package org.openChart.display 
{
	
	import org.openChart.meta.data.DataElement;
	import org.openChart.sprite.Color;
	import org.openChart.meta.data.DataGroup;
	import org.openChart.sprite.GroupSetSprite;
	import org.openChart.display.chart.DisplayElementFactor;
	import org.openChart.display.chart.DisplayPieElement;
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class DisplayGroup extends DisplayBaseObject
	{
		private var elements:Array = new Array();
		private var parent_:DisplayGroupSet;
		private var length_:uint = 0;
		private var total_:Number = 0;
		private var maxValue_:Number = Number.MIN_VALUE;
		private var minValue_:Number = Number.MAX_VALUE;
		
		private var width_:Number;
		private var groupName_:String;
		
		public function DisplayGroup( group:DataGroup, parent:DisplayGroupSet ) 
		{
			this.groupName_ = group.label;
			this.width_ = group.getStyle().width;
			parent_ = parent;
			super(group);
		}
		
		public override function initialize():void
		{
			clear();
			var group:DataGroup = dataModel as DataGroup;
			//trace(group);
			if (group)
			{
				for (var i:uint = 0; i < group.length; i++)
				{
					var element:DataElement = group.getAt(i);
					addElement(element);
				}
			}
		}
		
		public function getAt(index:uint):DisplayElement
		{
			if (index < 0 || index >= length_)
				throw new Error("index out of boundary error!");
			return elements[index] as DisplayElement;
		}
		
		public function addElement(element:DataElement):void
		{
			var displayElement:DisplayElement = DisplayElementFactor.create(element, this, parent.parent.chartType);
			elements.push(displayElement);
			length_++;
			if (element.getValue() > maxValue_)
				maxValue_ = element.getValue();
			if (element.getValue() < minValue_)
				minValue_ = element.getValue();
		}
		
		public function clear():void
		{
			
			for (var i:uint = 0; i < length_; i++)
			{
				elements.pop();
			}
			length_ = 0;
			maxValue_ = Number.MIN_VALUE;
			minValue_ = Number.MAX_VALUE;
		}
		
		public function sortByMiddleAngle():void
		{
			if (parent.parent.chartType.indexOf("pie") < 0)
				throw new Error("the group can't be sorted by middle angle");
			var group:Array = this.elements;
			var i:uint = 0;
			
			var displayElement:DisplayPieElement;
			var offsetAngles:Array = new Array();
			
			for (i = 0; i < group.length; i++)
			{
				displayElement = group[i] as DisplayPieElement;
				var offsetAngle:Number = (displayElement.beginAngle + displayElement.endAngle - 180) / 2;
				
				if (offsetAngle > 180)
					offsetAngle = 360 - offsetAngle;
				
				offsetAngles.push(offsetAngle);

			}
			var change:Boolean = true;
			for (i = 0; i < group.length; i++)
			{ 
				change = false;
				for (var j:uint = 0; j < group.length - 1; j++)
				{	
					if (offsetAngles[j] > offsetAngles[j + 1])
					{
						var angle:Number = offsetAngles[j];
						offsetAngles[j] = offsetAngles[j + 1];
						offsetAngles[j + 1] = angle;
						
						change = true;
						
						displayElement = group[j];
						group[j] = group[j + 1];
						group[j + 1] = displayElement;
					}
				}
				if (!change)
					break;
			}
			
		}
		
		public function get length():uint
		{
			return length_;
		}
		
		public function get total():Number
		{
			return total_;
		}
		
		public function get maxValue():Number
		{
			return maxValue_;
		}
		
		public function get minValue():Number
		{
			return minValue_;
		}
		
		public function set groupName( name:String ):void
		{
			this.groupName_ = name;
		}
		
		public function get groupName ():String
		{
			return this.groupName_;
		}
		
		public function get width():Number
		{
			return this.width_;
		}
		
		public function get parent():DisplayGroupSet
		{
			return parent_;
		}
		
		public function toString():String
		{
			var str:String = "\nObject:DisplayGroup ";
			str += "[width = " + width + "]";
			str += "[name = " + groupName + "]";
			str += "[elemens = ";
			for (var i:uint = 0; i < length; i++ )
			{
				str += elements[i];
			}
			str += "]";
			return str;
		}
	}
	
}